package stream;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @description: Steam演示
 * 1、Steam关注的是对数据的运算操作，与CPU打交道，集合关注的是数据的存储，与内存打交道
 * 2、Steam特性
 *
 * @author: apple
 * @date:  2022/1/3 11:14 下午
 * @version 1.0
 **/
public class StreamTest {

    // 方式一：通过Collection接口提供的默认方法来创建实例
    @Test
    public void test1() {

        // 创建数据源
        final List<Employee> data = Employee.getData();

        // 获取一个顺序流
        final Stream<Employee> stream = data.stream();
        System.out.println(stream);
        System.out.println(stream.getClass());

        // 获取一个并行流
        final Stream<Employee> employeeStream = data.parallelStream();
    }

    // 方式二：通过数组创建实例
    @Test
    public void test2() {
        int[] nums = {99,23,6,24,86,2};

        final IntStream stream = Arrays.stream(nums);
        Employee[] tem = new Employee[3];
        final Employee[] employees1 = Employee.getData().toArray(tem);
        final Stream<Employee> stream1 = Arrays.stream(employees1);

    }

    // 方式三：通过Steam类的of方法
    @Test
    public void test3() {
        final Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5);

        final Stream<String> a = Stream.of("a", "b");

        final Stream<Employee> employeeStream = Stream.of(new Employee());
    }


    static class Employee {
        private int id;
        private String name;
        private int age;

        private static List<Employee> list = null;

        public static List<Employee> getData() {


            if (list ==null) {

                synchronized (Employee.class) {
                    list = new ArrayList<>();
                }

            }

            list.add(new Employee(1,"张三",28));
            list.add(new Employee(2,"李四",27));
            list.add(new Employee(3,"五五",26));
            list.add(new Employee(4,"码云",26));
            list.add(new Employee(5,"马化腾",26));
            list.add(new Employee(5,"马化腾",30));

            return list;
        }

        public Employee() {

        }

        public Employee(int id, String name, int age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getName2() {
            return name;
        }


        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Employee employee = (Employee) o;
            return id == employee.id && age == employee.age && Objects.equals(name, employee.name);
        }

        @Override
        public int hashCode() {
            return Objects.hash(id, name, age);
        }

        @Override
        public String toString() {
            return "Employee{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }

        public boolean check(String n) {

            return n.length() > 2;
        }
    }
}
